package ar.com.framenet.base.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.xml.transform.TransformerException;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.w3c.dom.Document;
import org.w3c.dom.Node;
import org.w3c.dom.NodeList;

import ar.com.framenet.base.config.ApplicationConfiguration;
import ar.com.framenet.base.config.Constants;
import ar.com.framenet.base.exception.ConfigurationException;
import ar.com.framenet.base.util.XMLUtils;

@Service("generalService")
public class GeneralService {

	@Autowired
	ApplicationConfiguration appConfig;

	public String getMainTreeItemsAsXml() {
		System.out.println("GeneralService.getMainTreeItemsAsXml()... ");

		String xmlMenu = null;
		try {
			xmlMenu = XMLUtils.getXmlAsString(appConfig.getMenuDocument());
		} catch (ConfigurationException e) {
			e.printStackTrace();
		} catch (TransformerException e) {
			e.printStackTrace();
		}

		return xmlMenu;
	}

	public List<Map<String, Object>> getMainTreeItems() {
		System.out.println("GeneralService.getMainTreeItems()... ");

		Document menu = null;
		try {
			menu = appConfig.getMenuDocument();
		} catch (ConfigurationException e) {
			e.printStackTrace();
		}
		
		return getListItems(menu.getFirstChild());
	}
	
	public List<Map<String, Object>> getListItems(Node itemRoot) {

		List<Map<String, Object>> itemList = new ArrayList<Map<String, Object>>();

		NodeList items = itemRoot.getChildNodes();
		for (int i = 0; i < items.getLength(); i++)	{
			Node item = items.item(i);
			if (item.getNodeType() == Node.ELEMENT_NODE && item.getNodeName().equals(Constants.XML_MENU_ITEM_NAME))	{
				NodeList itemProps = item.getChildNodes();
				Map<String, Object> itemMap = new HashMap<String, Object>();
				for (int j = 0; j < itemProps.getLength(); j++)	{
					Node itemProp = (Node) itemProps.item(j);
					if (itemProp != null && itemProp.getNodeType() == Node.ELEMENT_NODE)	{

						if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_TEXT))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_TEXT, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_ID))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_ID, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_CLS))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_CLS, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_LEAF))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_LEAF, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_ENTITY))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_ENTITY, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_DAO))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_DAO, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_FRAME_DETAIL))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_FRAME_DETAIL, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_FRAME_GRIDMODEL))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_FRAME_GRIDMODEL, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_FRAME_DATAMODEL))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_FRAME_DATAMODEL, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_FRAME_FIELDSDATAMODEL))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_FRAME_FIELDSDATAMODEL, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEM_PROPERTY_FRAME_REPORT))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_FRAME_REPORT, itemProp.getTextContent());
						}else if (itemProp.getNodeName().equals(Constants.XML_MENU_ITEMS_NAME))	{
							itemMap.put(Constants.XML_MENU_ITEM_PROPERTY_CHILDREN, getListItems(itemProp));
						}
					}
				}
				
				itemList.add(itemMap);
			}
		}

		return itemList;		
		
	}
	
}
